package com.sp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sp.model.dto.BillDTO;
import com.sp.model.entity.Bill;
import com.sp.model.entity.BillIncomplete;
import com.sp.model.entity.PageResult;
import com.sp.model.entity.Result;
import com.sp.model.vo.BillVO;
import com.sp.service.BillService;
import com.sp.utils.ImageRecognition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import static com.sp.constant.BillMessageConstant.*;

/**
 * @author tong
 * @date 2024/3/16 20:39
 */
@Api(tags = "账单管理api接口")
@RestController
@RequestMapping("/bills")
public class BillController {

    @Autowired
    private BillService billService;

    @Autowired
    private ImageRecognition imageRecognition;

    @ApiOperation("根据车牌号添加未完成账单")
    @PostMapping("/incomplete")
    public Result<Object> addBillIncompleteByCarNumber(
            @ApiParam(name = "billDTO", value = "账单实体") @RequestBody BillDTO billDTO) {
        if (billService.addBillIncompleteByCarNumber(billDTO)) {
            return Result.success(ADD_BILL_SUCCESS);
        }
        return Result.error(ADD_BILL_FAIL);
    }

    @ApiOperation("车辆进入")
    @PatchMapping("/carIn")
    public Result<Object> addBillWithCarIn(
            @ApiParam(name = "img", value = "进入车辆车牌图像") @RequestBody Map<String, String> params) {
        String img = params.get("img");
        String carNumber = extractCarNumber(img);

        BillDTO billDTO = new BillDTO();
        billDTO.setCarNumber(carNumber);

        if (billService.addBillIncompleteByCarNumber(billDTO)) {
            return Result.success(ADD_BILL_SUCCESS);
        }
        return Result.error(ADD_BILL_FAIL);
    }

    /**
     * 根据图像提取车牌号，需联网
     *
     * @param img
     * @return
     */
    private String extractCarNumber(String img) {
        return imageRecognition.getCarNumber(img);
    }

    @ApiOperation("车辆离开")
    @PatchMapping("/carOut")
    public Result<Object> updateBillWithCarOut(
            @ApiParam(name = "img", value = "进入车辆车牌图像") @RequestBody Map<String, String> params) {
        String img = params.get("img");
        String carNumber = extractCarNumber(img);

        BillDTO billDTO = new BillDTO();
        billDTO.setCarNumber(carNumber);

        if (billService.addBillCompleteByCarNumber(billDTO)) {
            return Result.success(UPDATE_BILL_SUCCESS);
        }
        return Result.error(UPDATE_BILL_FAIL);
    }

    @ApiOperation("根据车牌号获取未完成账单")
    @GetMapping("/incomplete/{carNumber}")
    public Result<BillVO> getBillIncompleteByCarNumber(
            @ApiParam(name = "carNumber", value = "车牌号") @PathVariable String carNumber) {
        BillIncomplete billIncomplete = billService.selectBillIncompleteByCarNumber(carNumber);
        if (billIncomplete != null) {
            return Result.success(SELECT_BILL_SUCCESS, BeanUtil.copyProperties(billIncomplete, BillVO.class));
        }
        return Result.error(SELECT_BILL_FAIL);
    }

    @ApiOperation("根据id修改账单")
    @PutMapping
    public Result<Object> updateBillById(
            @ApiParam(name = "billDTO", value = "账单实体") @RequestBody BillDTO billDTO) {
        if (billService.updateBillById(billDTO)) {
            return Result.success(UPDATE_BILL_SUCCESS);
        }
        return Result.error(UPDATE_BILL_FAIL);
    }

    @ApiOperation("根据id删除账单")
    @DeleteMapping("/{ids}")
    public Result<Object> deleteBillById(
            @ApiParam(name = "ids", value = "账单id数组") @PathVariable List<Integer> ids) {
        if (billService.deleteBillById(ids)) {
            return Result.success(DELETE_BILL_SUCCESS);
        }
        return Result.error(DELETE_BILL_FAIL);
    }

    @ApiOperation("条件查询账单信息")
    @GetMapping("/pages")
    public Result<PageResult<BillVO>> pages(
            @ApiParam(name = "currentPage", value = "页码") Integer currentPage,
            @ApiParam(name = "pageSize", value = "每页条数") Integer pageSize,
            @ApiParam(name = "billDTO", value = "账单信息") BillDTO billDTO) {
        IPage<Bill> pages = billService.page(currentPage, pageSize, billDTO);
        if (pages != null) {
            List<BillVO> billVOS = BeanUtil.copyToList(pages.getRecords(), BillVO.class);

            return Result.success(SELECT_BILL_SUCCESS
                    , new PageResult<>(pages.getSize(), pages.getTotal(), pages.getCurrent(), pages.getPages(), billVOS));
        }
        return Result.error(SELECT_BILL_FAIL);
    }

    @ApiOperation("统计账单信息（钱）")
    @GetMapping("/moneyChart")
    public Result<List<Map<String, String>>> moneyChart(
            @ApiParam(name = "createTime", value = "目标创建时间") LocalDateTime createTime,
            @ApiParam(name = "range", value = "时间范围") Integer range) {
        List<Map<String, String>> data = billService.getMoneyData(createTime, range);

        if (data != null) {
            return Result.success(SELECT_BILL_SUCCESS, data);
        }
        return Result.error(SYSTEM_ERROR);
    }

    @ApiOperation("统计账单信息（数量）")
    @GetMapping("/amountChart")
    public Result<List<Map<String, String>>> amountChart(
            @ApiParam(name = "createTime", value = "目标时间") LocalDateTime createTime,
            @ApiParam(name = "range", value = "时间范围") Integer range,
            @ApiParam(name = "status", value = "账单状态") String status) {
        List<Map<String, String>> data = billService.getAmountData(createTime, range, status);

        if (data != null) {
            return Result.success(SELECT_BILL_SUCCESS, data);
        }
        return Result.error(SYSTEM_ERROR);
    }

}
